package com.wuyou.mid;

/**
 * @Description 1410. HTML 实体解析器
 * @Author yh.zhang
 * @Date 2023/11/23 9:09
 */
public class EntityParser {

    static String[] checkArr = {"&quot;", "\"", "&apos;", "'", "&amp;", "&", "&gt;", ">", "&lt;", "<", "&frasl;", "/"};

    public static void main(String[] args) {
//        String text = "&amp; is an HTML entity but &ambassador; is not.";
        String text = "&&gt;";

        System.out.println(solution_2(text));
    }

    public static String solution_2(String text) {
        StringBuilder ans = new StringBuilder();
        String[] sArr = text.split(" ");
        for (int i = 0; i < sArr.length; i++) {
            String s = sArr[i];
            StringBuilder s1 = new StringBuilder();
            if (s.indexOf(";") != -1 && s.indexOf("&") != -1) {
                boolean off = true;
                int begin = 0, end = 0;
                char[] chars = s.toCharArray();
                for (int j = 0; j < chars.length; j++) {
                    if (chars[j] == '&') {
                        if (off) {
                            off = false;
                        } else {
                            s1.append(new String(chars, begin, j - begin));
                        }
                        begin = j;
                    } else if (chars[j] == ';' && !off) {
                        off = true;
                        end = j;
                        String s3 = new String(chars, begin, end - begin + 1);
                        String check = check(s3);
                        s1.append(check);
                    } else if (off) {
                        s1.append("" + chars[j]);
                    }
                }
                ans.append(s1);
                s1 = new StringBuilder();
            } else {
                ans.append(s);
            }
            if (i != sArr.length - 1) {
                ans.append(" ");
            }
        }
        return ans.toString();
    }


    public static String solution_1(String text) {
        StringBuilder ans = new StringBuilder();
        String[] sArr = text.split(" ");
        for (int i = 0; i < sArr.length; i++) {
            String temp = sArr[i];
            boolean flag = false;
            char[] tempChars = temp.toCharArray();
            int begin = 0, end = 0;
            for (int j = 0; j < tempChars.length; j++) {
                if (tempChars[j] == '&') {
                    flag = true;
                    begin = j;
                } else if (tempChars[j] == ';' && flag) {
                    flag = false;
                    end = j;
                    String s = new String(tempChars, begin, end - begin + 1);
                    String checkRes = check(s);
                    temp = temp.replace(s, checkRes);
                }
            }
            ans.append(temp);
            if (i != sArr.length - 1) {
                ans.append(" ");
            }
        }
        return ans.toString();
    }

    public static String check(String checkStr) {
        for (int i = 0; i < checkArr.length; i += 2) {
            if (checkStr.equals(checkArr[i])) {
                return checkArr[i + 1];
            }
        }
        return checkStr;
    }
}
